package com.sgb.kjwl.view.ui.activity.pick;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;

import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;

import com.google.android.material.tabs.TabLayout;
import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityPickHallBinding;
import com.sgb.kjwl.view.event.PickEvent;
import com.sgb.kjwl.view.ui.activity.material.hall.NewPickBiddingListFragment;
import com.sgb.kjwl.view.ui.activity.material.hall.NewPickSigningUpListFragment;
import com.sgb.kjwl.view.ui.activity.material.hall.PickNoticeFragment;
import com.sgb.link.view.adapter.PickNoticeAdapter;
import com.sgb.kjwl.view.ui.fragment.pick.PickHallAllFragment;
import com.sgb.kjwl.view.ui.fragment.pick.PickHallBidingFragment;
import com.sgb.zhaocai.utils.PickUtil;
import com.sgb.zhaocai.view.demand.fragment.DemandForecastListFragment;
import com.swgk.core.util.CommonUtils;
import com.swgk.core.util.MToast;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;

//招标大厅
public class PickHallActivity3 extends AppActivity {

    private ActivityPickHallBinding binding;

    private ArrayList<String> titles = new ArrayList<>();
    private ArrayList<Fragment> mFragments;
    private InputMethodManager im;
    private PickHallBidingFragment pickHallBidingFragment;
    private NewPickBiddingListFragment newPickBiddingListFragment;
    private NewPickSigningUpListFragment newPickSigningUpListFragment;
    private PickNoticeFragment pickNoticeFragment;
    private PickHallAllFragment pickHallAllFragment;
    private boolean isSearching = false;
    private String searchName = "";
    private boolean isMap = true;//是否显示地图模式
    private int fromWhere = 0;
    private int tabPosition = 0;
    private DemandForecastListFragment demandforecastlistfragment;

    public static void start(Context context) {
        start(context, 0);
    }

    public static void start(Context context, boolean isMap, int fromWhere) {
        Intent intent = new Intent(context, PickHallActivity3.class);
        Bundle bundle = new Bundle();
        bundle.putBoolean("isMap", isMap);
        bundle.putInt("fromWhere", fromWhere);
        intent.putExtras(bundle);
        context.startActivity(intent);
    }

    public static void start(Context context, int fromWhere) {
        Intent intent = new Intent(context, PickHallActivity3.class);
        Bundle bundle = new Bundle();
        bundle.putInt("fromWhere", fromWhere);
        intent.putExtras(bundle);
        context.startActivity(intent);
    }

    public static void start(Context context, int fromWhere, int tabPosition) {
        Intent intent = new Intent(context, PickHallActivity3.class);
        Bundle bundle = new Bundle();
        bundle.putInt("fromWhere", fromWhere);
        bundle.putInt("tabPosition", tabPosition);
        intent.putExtras(bundle);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Override
    protected void initView() {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_pick_hall);
        binding.include.ivBack.setOnClickListener(view -> finish());

        im = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    }

    @Override
    protected void init() {

        if (getIntent() != null) {
            isMap = getIntent().getBooleanExtra("isMap", true);
            fromWhere = getIntent().getIntExtra("fromWhere", 0);
            tabPosition = getIntent().getIntExtra("tabPosition", 0);
        }
        titles.add("全部");
        if (fromWhere == 0) {   //招标大厅
            titles.add("招标公告");
            titles.add("直接竞价");
            titles.add("需求预告");
        } else if (fromWhere == 1) {  //招标公告
            titles.add("招标报名");
            titles.add("直接竞价");
        }
        titles.add("招标预告");
        binding.include.tvSearchDelete.setOnClickListener(this);
        binding.include.etView.setHint("搜索招标名称");
        binding.include.rightView.setOnClickListener(this);
        binding.include.rightView.setText("发布");
        binding.include.tvSearch.setOnClickListener(this);
        binding.include.tvSearch.setText("搜索");
        binding.include.etView.setTextColor(this.getResources().getColor(R.color.color_tab_one));
        binding.include.etView.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void afterTextChanged(Editable s) {
                if (TextUtils.isEmpty(s)) {
                    binding.include.tvSearchDelete.setVisibility(View.GONE);
                } else {
                    binding.include.tvSearchDelete.setVisibility(View.VISIBLE);
                }
            }
        });
        mFragments = new ArrayList<>();
        if (fromWhere == 0) {     //招标大厅
            mFragments.add(PickHallAllFragment.newInstance(0, tabPosition, isMap));
            mFragments.add(PickHallBidingFragment.newInstance("2", isMap));
            mFragments.add(NewPickBiddingListFragment.newInstance("3"));
            mFragments.add(DemandForecastListFragment.newInstance("1"));
        } else if (fromWhere == 1) {  //招标公告
            mFragments.add(PickHallAllFragment.newInstance(1));
            mFragments.add(NewPickSigningUpListFragment.newInstance("3"));
            mFragments.add(NewPickBiddingListFragment.newInstance("3"));
        }
        mFragments.add(PickNoticeFragment.newInstance("3"));
        PickNoticeAdapter pagerAdapter = new PickNoticeAdapter(getSupportFragmentManager(), mFragments, titles);
        binding.viewpager.setAdapter(pagerAdapter);
        binding.viewpager.setOffscreenPageLimit(mFragments.size());
        binding.tabLayout.setupWithViewPager(binding.viewpager);
        binding.tabLayout.setTabMode(TabLayout.MODE_SCROLLABLE);
        binding.tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                changeTabData(tab.getPosition());
                Fragment fragment = mFragments.get(tab.getPosition());
                if (fragment != null && fragment instanceof NewPickBiddingListFragment) {
                    ((NewPickBiddingListFragment) fragment).onInvisible();
                }
                if (fragment != null && fragment instanceof PickHallBidingFragment) {
                    ((PickHallBidingFragment) fragment).onInvisible();
                }
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {

            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });

        if (fromWhere == 0) {     //招标大厅
            pickHallAllFragment = (PickHallAllFragment) mFragments.get(0);
            pickHallBidingFragment = (PickHallBidingFragment) mFragments.get(1);
            demandforecastlistfragment = (DemandForecastListFragment) mFragments.get(3);
        } else if (fromWhere == 1) {  //招标公告
            pickHallAllFragment = (PickHallAllFragment) mFragments.get(0);
            newPickSigningUpListFragment = (NewPickSigningUpListFragment) mFragments.get(1);
        }
        newPickBiddingListFragment = (NewPickBiddingListFragment) mFragments.get(2);
        pickNoticeFragment = (PickNoticeFragment) mFragments.get(mFragments.size() - 1);


        binding.include.etView.setImeOptions(EditorInfo.IME_ACTION_SEARCH);
        binding.include.etView.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                if (isSearching) return true;
                if (keyCode == KeyEvent.KEYCODE_ENTER && event.getAction() == KeyEvent.ACTION_DOWN) {
                    if (TextUtils.isEmpty(binding.include.etView.getText().toString())) {
                        MToast.showToast(PickHallActivity3.this, "请输入搜索内容");
                        return true;
                    }
                    if (searchName.equals(binding.include.etView.getText().toString())) {
                        MToast.showToast(PickHallActivity3.this, "请输入不同的搜索内容");
                        return true;
                    }
                    isSearching = true;
                    searchName = binding.include.etView.getText().toString();
                    if (!TextUtils.isEmpty(searchName)) {
                        changeTabData(binding.viewpager.getCurrentItem());
                        isSearching = false;
                    }
                }
                return false;
            }
        });
        if (isMap) {
            binding.viewpager.setCurrentItem(0);
        } else {
            if (fromWhere == 0) {
                binding.viewpager.setCurrentItem(0);
            }
        }
    }

    private void changeTabData(int position) {
        switch (position) {
            case 0:
                binding.include.etView.setHint("搜索招标名称");
                pickHallAllFragment.updateData(searchName, fromWhere);
                break;
            case 1:
                binding.include.etView.setHint("搜索招标名称");
                if (0 == fromWhere) {
                    pickHallBidingFragment.updateData(searchName);
                } else if (1 == fromWhere) {  //招标公告
                    newPickSigningUpListFragment.updateData(searchName);
                }
                break;
            case 2:
                binding.include.etView.setHint("搜索竞价名称");
                newPickBiddingListFragment.updateData(searchName);
                break;
            case 3:
                if (0 == fromWhere) {
                    binding.include.etView.setHint("搜索关键字");
                    demandforecastlistfragment.updateData(searchName);
                } else if (1 == fromWhere) {  //招标公告
                    binding.include.etView.setHint("搜索招标名称");
                    pickNoticeFragment.updateData(searchName);
                }
                break;
            case 4:
                binding.include.etView.setHint("搜索招标名称");
                pickNoticeFragment.updateData(searchName);
                break;
        }
    }


    /**
     * 判断当前界面为某个fragment
     *
     * @return
     */
    public boolean isShow(String type) {

        if (type.equals("招标公告") || type.equals("招标报名")) {
            return binding.viewpager.getCurrentItem() == 1;
        } else if (type.equals("直接竞价")) {
            return binding.viewpager.getCurrentItem() == 2;
        } else if (type.equals("全部")) {
            return binding.viewpager.getCurrentItem() == 0;
        }
        return false;
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        if (null != im) {
            im.hideSoftInputFromWindow(binding.include.etView.getWindowToken(), 0);
        }
        return super.dispatchTouchEvent(ev);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.right_view:
                if (CommonUtils.buttonClick()) {
                    return;
                } else {
                    PickUtil.showBottomZhaoCaiDialog(this);
                }
                break;
            case R.id.tv_search:
                searchName = binding.include.etView.getText().toString();
                if (!TextUtils.isEmpty(searchName)) {
                    changeTabData(binding.viewpager.getCurrentItem());
                }
                break;
            case R.id.tv_search_delete:
                binding.include.etView.setText("");
                searchName = "";
                changeTabData(binding.viewpager.getCurrentItem());
                break;
        }
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMessageEvent(PickEvent event) {
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
